Intro

model-viewer

This package wraps the excellent web component <model-viewer> to make it easier to use/consume in toddle apps.

The model-viewer allows users to interact with complex 3D models provided as .glb or .gltf files.

Demo

Try picking different models below

You can find more models here

Components

  • model-viewer

    The main component that wraps the web component <model-viewer>. It doesn't expose everything from the original API for now. See available attributes and events below.

    Attributes

    srcDocsURL to the .glb or .gltf file
    altDocsText for screen readers as an alternative representation of the 3D model.
    posterDocsAn image URL for an image to display until the model is loaded and rendered.
    loadingDocsLoading strategy for the model. Can be "auto", "lazy" or "eager" - defaults to "auto"
    with-credentialsDocsUses cookies when fetching the 3D model from the "src" attribute
    disable-panDocsDisables panning
    disable-tapDocsDisables tapping
    disable-zoomDocsDisables zoom
    auto-rotateDocsAutomatically rotates the 3D model
    auto-fitSets width and height to 100% of the <model-viewer> element

    Events

    errorDocsEvent that indicates an error happened - for instance if the 3D model couldn't be loaded
    loadDocsFires when the model has been loaded
  • effect-composer

    Supports composing multiple effects (see components below)

    Attributes

    render-modeDocsSupports "performance" or "quality"
    msaaDocsControls the number of samples used. Can be any power of 2, defaults to 0.
  • bloom-effect

    This effect creates a glowing light-bleed effect around bright objects.

    Attributes

    strengthDocsThe strength of the bloom effect. Takes any positive number, defaults to 1
    thresholdDocsThe pixel brightness threshold below which objects will not glow. Takes a number in the range [0.0, 1.0]. Defaults to 1.0
    radiusDocsHow far the light bleed extends. A number in the range [0.0, 1.0]. Defaults to 0.85
    smoothingDocsThe roll-off of the light-bleed. A number in the range [0.0, 1.0]. Defaults to 0.025
  • pixelate-effect

    This effect pixelates the entire scene.

    Attributes

    strengthDocsThe pixel size; draw one pixel for every granularity pixels. Higher value = lower resolution. Takes any positive number. Defaults to 10
  • color-grade-effect

    This effect alters the look of the entire scene.

    Attributes

    tonemappingDocsOptions: reinhard, reinhard2, reinhard2_adaptive, optimized_cineon, aces_filmic, none. Defaults to aces_filmic
    brightnessDocsThe brightness of the entire canvas. 0.0 is the original brightness, 1.0 is completely white, -1.0 is completely black. Takes a number in the range [0.0, 1.0]. Defaults to 0.0
    contrastDocsThe contrast of the entire canvas. 0.0 is no change, 1.0 is only black or white pixels, -1.0 is only grey pixels. Takes a number in the range [-1.0, 1.0]. Defaults to 0.0
    saturationDocsThe saturation of the entire canvas. 0.0 is no change, 1.0 means all pixels are 100% saturation, -1.0 means all pixels are 0% saturation. Takes a number in the range [-1.0, 1.0]. Defaults to 0.0
    hueDocsThe hue color shift in radians of the entire canvas. 0.0 is no change, any higher value is a shift along the color wheel of hue radians. This property is wrapping, meaning that if you set it above the max it resets to the minimum and vice-versa. Takes a number in the range [0.0, 2 * PI]. Defaults to 0.0
  • outline-effect

    This selective effect draws outlines around selected objects.

    Attributes

    strengthDocsThe edge thickness. Takes any positive number. Defaults to 1.0
    colorDocsThe edge color. Takes any string or RGB #-hexadecimal color.
    smoothingDocsThe edge blur and roll-off. Takes a number in the range [0, 6]
  • glitch-effect

    This effect causes chromatic abberation and screen tearing glitches on the scene.

    Attributes

    strengthDocsThe strength of the glitches. Higher value means more significant glitches. Takes a number in the range [0.0, 1.0]. Defaults to 0.5
    modeDocsThe rate of the glitches. Sporadic glitches occur once every 1.0-3.5 seconds, and last between 0.5-1.0 seconds. Constant glitches have no delay, and is best used with very low strength values. Takes either sporadic or constant
  • ssao-effect

    This effect simulates soft shadows in between geometries in the scene.

    Attributes

    strengthDocsThe strength of the shadow occlusions. Higher value means darker shadows. Takes any positive number. Defaults to 2.0
  • smaa-effect

    This effect runs Subpixel Morphological Antialiasing (SMAA) on the scene.

    Attributes

    qualityDocsThe quality of the anti-aliasing. Higher quality is less performant. Accepts low, medium, high, ultra. Defaults to medium

Formulas

  • getDimensions

    Returns the dimensions (x, y, z) of the loaded 3D model

    Docs

    Arguments

    idid of the model-viewer instance
  • toDataUrl

    Returns a dataUrl for a screenshot of the model viewer

    Docs

    Arguments

    idid of the model-viewer instance